<?php
class ControllerUserUser extends AdminController {

  	public function index() {
		M('user/user');
    	$this->getList();
  	}

  	public function insert() {
		M('user/user');

    	if ($this->request->isPost() && $this->validateForm()) {
			$this->model_user_user->addUser($this->request->post);

			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('user/user'));
    	}
    	$this->getForm();
  	}

  	public function update() {
		M('user/user');

    	if ($this->request->isPost() && $this->validateForm()) {
			$this->model_user_user->editUser($this->request->get['user_id'], $this->request->post);

			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('user/user'));
		}
    	$this->getForm();
  	}

  	public function delete() {
		M('user/user');

    	if (isset($this->request->post['selected']) && $this->validateDelete()) {
      		foreach ($this->request->post['selected'] as $user_id) {
				$this->model_user_user->deleteUser($user_id);
			}
			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('user/user'));
    	}
    	$this->getList();
  	}

  	public function smartedit() {
  		if ($this->hasMP()) {
	  		$user_id = (int) G('id');
	  		$field = G('field');
	  		$value = G('val');
	  		$rs = $this->db->update('user', array($field => $value), "user_id=$user_id");
  		}
  		else $rs = 0;
  		$this->response->setOutput(json_encode(array('status' => $rs)));
  	}
  	
  	protected function getList() {
		$this->document->setTitle($this->_['heading_title']);

		$qf = new Query_filter();

		$user_total = $this->model_user_user->getTotalUsers();
		$this->data['users'] = $this->model_user_user->getUsers($qf);

		$this->data['pagination'] = $this->pagination($user_total)->render();
		
		$this->display('user/user_list.tpl');
  	}

	protected function getForm() {
		$this->document->setTitle($this->_['heading_title']);

		if (isset($this->request->get['user_id'])) {
			$this->data['action'] = UA('user/user/update', 'user_id=' . $this->request->get['user_id']);
			if (!$this->request->isPost()) {
				$this->vo->data = $this->model_user_user->getUser($this->request->get['user_id']);
			}	
		} else {
			$this->data['action'] = UA('user/user/insert');
		}

		M('user/user_group');
    	$this->data['user_groups'] = $this->model_user_user_group->getUserGroups();

		$this->display('user/user_form.tpl');
  	}

  	protected function validateForm() {
		$user_info = $this->model_user_user->getUserByUsername($this->request->post['username']);
		if ($user_info && (!isset($this->request->get['user_id'])
			|| ($this->request->get['user_id'] != $user_info['user_id']))) {
			$this->setMessage('error_warning', $this->_['error_exists']);
			return false;
		}

		$this->load->library('form_validation', true);
    	$this->form_validation->set_rules('username', '', 'required|range_length[3,20]', $this->_['error_username']);
		$this->form_validation->set_rules('firstname', '', 'required|range_length[1,32]', $this->_['error_firstname']);
		$this->form_validation->set_rules('lastname', '', 'required|range_length[1,32]', $this->_['error_lastname']);

    	if ($this->request->post['password'] || (!isset($this->request->get['user_id']))) {
			$this->form_validation->set_rules('password', '', 'required|range_length[4,20]', $this->_['error_password']);
			$this->form_validation->set_rules('confirm', '', 'required|matches[password]', $this->_['error_confirm']);
    	}
		return $this->form_validation->run();
  	}

  	protected function validateDelete() {
		foreach ($this->request->post['selected'] as $user_id) {
			if ($this->user->getId() == $user_id) {
				$this->setMessage('error_warning', $this->_['error_account']);
				return false;
			}
		}
		return true;
  	}
}
?>